<template>
    <el-dialog ref="dialogform" v-if="dialogVisible" :title="addEditName + title" :visible.sync="dialogVisible"
        width="90%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-row>
                <el-col :span="8"><el-form-item label="网站名称" prop="headerName">
                        <el-input v-model="ruleForm.headerName" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="顶部栏标题" prop="topArticle">
                        <el-input v-model="ruleForm.topArticle" class="w100p" maxlength="6" show-word-limit clearable
                            placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="右侧栏1#名称" prop="rightColumnOne">
                        <el-input v-model="ruleForm.rightColumnOne" class="w100p" maxlength="6" show-word-limit
                            clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="右侧栏2#名称" prop="rightColumnTwo">
                        <el-input v-model="ruleForm.rightColumnTwo" class="w100p" maxlength="6" show-word-limit
                            clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="主办单位" prop="organizer">
                        <el-input v-model="ruleForm.organizer" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="联系电话" prop="phoneNumber">
                        <el-input v-model="ruleForm.phoneNumber" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="地址" prop="address">
                        <el-input v-model="ruleForm.address" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="导航栏颜色">
                        <div style="display: flex;justify-content: flex-start;align-items: center;">
                            <el-color-picker v-model="ruleForm.navigationColor" show-alpha color-format="hex"
                                placeholder="请选择颜色"
                                @change="(e) => { handleColorChange(e, 'navigationColor') }"></el-color-picker>
                            <span style="padding-left: 15px;">{{ ruleForm.navigationColor || '暂无颜色' }}</span>
                        </div>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="导航栏悬停色">
                        <div style="display: flex;justify-content: flex-start;align-items: center;">
                            <el-color-picker v-model="ruleForm.hoverColor" show-alpha color-format="hex"
                                placeholder="请选择颜色"
                                @change="(e) => { handleColorChange(e, 'hoverColor') }"></el-color-picker>
                            <span style="padding-left: 15px;">{{ ruleForm.hoverColor || '暂无颜色' }}</span>
                        </div>
                    </el-form-item></el-col>
                <el-col :span="24">
                    <el-col :span="8"><el-form-item label="页眉图" prop="headerImage">
                            <UpLoadImage v-if="dialogVisible"
                                @getFileIdList="(e) => { getFileIdList(e, 'headerImage') }" :idList="headerImageList"
                                :limit="1">
                            </UpLoadImage>
                        </el-form-item></el-col>
                    <el-col :span="8"><el-form-item label="栏目图" prop="columnChart">
                            <UpLoadImage v-if="dialogVisible"
                                @getFileIdList="(e) => { getFileIdList(e, 'columnChart') }" :idList="columnChartList"
                                :limit="1">
                            </UpLoadImage>
                        </el-form-item></el-col>
                    <el-col :span="8"><el-form-item label="背景图" prop="backgroundImage">
                            <UpLoadImage v-if="dialogVisible"
                                @getFileIdList="(e) => { getFileIdList(e, 'backgroundImage') }"
                                :idList="backgroundImageList" :limit="1">
                            </UpLoadImage>
                        </el-form-item></el-col>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="黑白名单" prop="rosterType">
                        <el-select v-model="ruleForm.rosterType" class="w100p" clearable filterable placeholder="请选择">
                            <el-option v-for="item, index in enumList" :key="index" :label="item.text"
                                :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-if="ruleForm.rosterType !== 'RELEASE'">
                    <el-form-item label="Ip区间" prop="ipAdressVoList">
                        <div class="IpModule" v-for="item, index in ruleForm.ipAdressVoList" :key="index">
                            <IpModule @change="(ip) => getIp(ip, index, 'startIp')" :ipAddress="item.startIp">
                            </IpModule>
                            <span>至</span>
                            <IpModule @change="(ip) => getIp(ip, index, 'endIp')" :ipAddress="item.endIp">
                            </IpModule>
                            <!-- <span class="label">开始段:</span>
                            <el-input type="number" class="input" v-model="item.startInt" maxlength="3"
                                @input="item.startInt = item.startInt.replace(/[^\d]/g, '')"
                                @blur="syncIp(item.startInt, index, 'startInt')" />
                            <span class="label">结束段:</span>
                            <el-input type="number" class="input" v-model="item.endInt" maxlength="3"
                                @input="item.endInt = item.endInt.replace(/[^\d]/g, '')"
                                @blur="syncIp(item.endInt, index, 'endInt')" /> -->
                            <div class="btnList">
                                <span v-show="ruleForm.ipAdressVoList.length == index + 1"
                                    @click="addIp(index)">添加</span>
                                <span class="del" v-show="ruleForm.ipAdressVoList.length > 1"
                                    @click="delIp(index)">删除</span>
                            </div>
                        </div>
                    </el-form-item>
                </el-col>
                <el-col :span="8"><el-form-item label="值班列一" prop="dutyOne">
                        <el-input v-model="ruleForm.dutyOne" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="值班列二" prop="dutyTwo">
                        <el-input v-model="ruleForm.dutyTwo" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
                <el-col :span="8"><el-form-item label="值班列三" prop="dutyThree">
                        <el-input v-model="ruleForm.dutyThree" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item></el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import UpLoadImage from "@/components/UpLoadImage";
import IpModule from '@/components/IpModule';
import endPage from "@/api/endPage"
export default {
    components: { UpLoadImage, IpModule },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        articleList: {
            type: Array,
            default: () => {
                return []
            }
        },
        enumList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        // 自定义验证规则，检查网址是否为有效的URL
        var validateUrl = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('网址不能为空'));
            }
            // 正则表达式判断网址格式
            const reg = /(http|https):\/\/([\w.]+\/?)\S*/
            if (reg.test(value)) {
                callback();
            } else {
                callback('请输入正确的网址');
            }
        };
        return {
            backgroundImageList: [],
            columnChartList: [],
            headerImageList: [],
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            color: '',
            rules: {
                headerName: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                organizer: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                phoneNumber: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                backgroundImage: [
                    { required: true, message: '请上传图片', trigger: 'change' }
                ],
                columnChart: [
                    { required: true, message: '请上传图片', trigger: 'change' }
                ],
                headerImage: [
                    { required: true, message: '请上传图片', trigger: 'change' }
                ],
                address: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                navigationColor: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                hoverColor: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
            }
        }
    },
    computed: {},
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        handleColorChange(value, key) {
            this.ruleForm[key] = value;
            console.log('颜色变化了', value);
        },
        initData() {
            this.ruleForm = { ...this.data };
            if (!this.ruleForm.rosterType) {
                this.$set(this.ruleForm, 'rosterType', 'RELEASE');
            }
            this.backgroundImageList = this.data.backgroundImage ? [this.data.backgroundImage] : [];
            this.headerImageList = this.data.headerImage ? [this.data.headerImage] : [];
            this.columnChartList = this.data.columnChart ? [this.data.columnChart] : [];
            this.addEditApi = endPage.add;
            this.addEditName = '编辑';
            if (!this.ruleForm.ipAdressVoList || this.ruleForm.ipAdressVoList.length == 0) {
                this.$set(this.ruleForm, 'ipAdressVoList', [{ startIp: '', endIp: '' }]);
            }
        },
        getIp(ip, index, key) {
            if (!this.ruleForm.ipAdressVoList[index]) {
                this.$set(this.ruleForm.ipAdressVoList, index, { startIp: '', endIp: '' })
            }
            this.$set(this.ruleForm.ipAdressVoList[index], key, ip)
        },
        addIp(index) {
            this.$set(this.ruleForm.ipAdressVoList, index + 1, { startIp: '', endIp: '' })
        },
        delIp(index) {
            this.ruleForm.ipAdressVoList.splice(index, 1)
        },
        getFileIdList(data, key) {
            let id = data.fileIdList && data.fileIdList.length > 0 ? data.fileIdList[0] : ''
            this.$set(this.ruleForm, key, id)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let success = res.code == 0;
                        let msg = success ? '操作成功' : '操作失败';
                        this.$message[success ? 'success' : 'warning'](res.msg || msg);
                        success && this.cancel();
                    }).catch(err => {
                        console.log(err);
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.fileIdList = [];
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.IpModule {
    display: flex;
    justify-content: flex-start;
    flex-wrap: nowrap;
    line-height: 38px;

    .input {
        width: 200px;
    }

    .label {
        display: inline-block;
        width: 100px;
        padding: 0 5px;
        text-align: right;
        cursor: pointer;
    }

    .btnList {
        width: 200px;
        display: flex;
        justify-content: flex-start;
        padding-left: 15px;
        color: rgb(27, 143, 244);

        span {
            padding-left: 10px;
        }

        .del {
            color: red;
            padding-right: 5px;
        }
    }
}
</style>